<template>
  <div
    v-loading="loading"
    class="home_container"
    ref="container"
    @wheel="handlerWhell"
  >
    <ul
      class="carousel_container"
      :style="{ marginTop }"
      @transitionend="handlerTransitionEnd"
    >
      <li v-for="item in banners" :key="item.id">
        <CarouselItem :item="item"></CarouselItem>
      </li>
    </ul>
    <div @click="swithTo(index - 1)" class="icon  icon-up" v-show="index >= 1">
      <Icon type="arrowUp" />
    </div>
    <div
      @click="swithTo(index + 1)"
      class="icon icon-down"
      v-show="index < banners.length - 1"
    >
      <Icon type="arrowDown" />
    </div>
    <ul class="indicator">
      <li
        :class="{ active: index === i }"
        v-for="(item, i) in banners"
        :key="item.id"
        @click="swithTo(i)"
      ></li>
    </ul>

    <!-- <Loading v-if="loading" /> -->
  </div>
</template>

<style scoped lang="less">
@import "~@/styles/mixin.less";
@import "~@/styles/var.less";
.home_container {
  width: 100%;
  height: 100%;
  overflow: hidden;
  position: relative;
  ul {
    margin: 0;
    list-style: none;
    padding: 0;
  }
}
.carousel_container {
  width: 100%;
  height: 100%;
  transition: 500ms;
  li {
    width: 100%;
    height: 100%;
  }
}
.icon {
  .self-center();
  @gay: 15px;
  font-size: 30px;
  color: @gray;
  cursor: pointer;
  transform: translateX(-50%);
  &.icon-up {
    top: @gay;
    animation: jump-up 1s infinite;
  }
  &.icon-down {
    top: auto;
    bottom: @gay;
    animation: jump-down 1s infinite;
  }

  @jump: 5px;
  @keyframes jump-up {
    0% {
      transform: translate(-50%, @jump);
    }
    50% {
      transform: translate(-50%, -@jump);
    }
    100% {
      transform: translate(-50%, @jump);
    }
  }
  @keyframes jump-down {
    0% {
      transform: translate(-50%, -@jump);
    }
    50% {
      transform: translate(-50%, @jump);
    }
    100% {
      transform: translate(-50%, -@jump);
    }
  }
}
.indicator {
  .self-center();
  transform: translateY(-50%);
  left: auto;
  right: 20px;
  li {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background-color: @words;
    cursor: pointer;
    margin-bottom: 10px;
    border: 1px solid #fff;
    box-sizing: border-box;
    transition: 0.5s;

    &.active {
      background-color: #fff;
    }
  }
}
</style>

<script>
import CarouselItem from "./components/Carouselitem.vue";
import Icon from "@/components/Icon";

import { getBanners } from "@/api";

export default {
  components: {
    CarouselItem,
    Icon,
  },
  data() {
    return {
      banners: [],
      index: 0,
      containerHeight: 0,
      switching: false,
      loading: true,
    };
  },
  methods: {
    swithTo(i) {
      this.index = i;
    },
    handlerWhell(e) {
      if (this.switching) {
        return;
      }
      if (e.deltaY < -5 && this.index > 0) {
        this.switching = true;
        this.index--;
      } else if (e.deltaY > 5 && this.index < this.banners.length - 1) {
        this.switching = true;
        this.index++;
      }
    },
    handlerTransitionEnd() {
      this.switching = false;
    },
    handlerResize() {
      this.containerHeight = this.$refs.container.clientHeight;
    },
  },
  mounted() {
    getBanners().then((res) => {
      this.loading = false;
      this.banners = res;
    });
    this.containerHeight = this.$refs.container.clientHeight;
    window.addEventListener("resize", this.handlerResize);
  },
  destroyed() {
    window.removeEventListener("resize", this.handlerResize);
  },
  computed: {
    marginTop() {
      return -this.index * this.containerHeight + "px";
    },
  },
};
</script>
